home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / UTILS / UTILS.C < prev   
Text File  |  1992-12-02  |  8KB  |  366 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030790    :    rewrote CopyStr to not use BlockMove
  54.  *    SPK 012290    :    Initial
  55.  */
  56.  
  57. #include    "SystemPub.h"
  58.  
  59. /*******************************************************************/
  60.  
  61. OsType    StrToType( char *str )
  62. {
  63. OsType    *tp;
  64. int        i;
  65.  
  66.     for( i = 0; i < 4; i++ )    
  67.         if( str[ i ] == '\0' )
  68.             for( ; i < 4; i++ )
  69.                 str[ i ] = ' ';
  70.  
  71.     tp = (OsType *) str;
  72.     return( *tp );
  73. }
  74.  
  75. void    TypeToStr( OsType type, char *str )
  76. {
  77. char    *t;
  78. int        index;
  79.  
  80.     t = (char*) &(type);
  81.     
  82.     for( index = 0; index < 4; index++ )
  83.         str[ index ]  = t[ index ];
  84.         
  85.     str[ index ] = '\0';
  86. }
  87.  
  88. /******* CopyStr - copy a pascal string into another ***************/
  89.  
  90. void        CopyStr( char *srcStr, char *dstStr )
  91. {
  92. unsigned int16    srcLen = (unsigned) srcStr[0]+1;
  93.  
  94.     while( srcLen-- )
  95.         *dstStr++ = *srcStr++;
  96. }
  97.  
  98. /********************************************************************/
  99. /*                             Dialog utilities                        */
  100. /********************************************************************/
  101.  
  102. void    SetEText( DialogPtr dPtr, int16 item, char *str )
  103. {
  104. int16    theType;
  105. Handle     theItem;
  106. Rect     theBox;
  107.  
  108.     GetDItem (dPtr, item, &theType, &theItem, &theBox);
  109.     if( theItem != NULL )
  110.         SetIText (theItem, str);
  111. }
  112.  
  113. /*******************************************************************/
  114.  
  115. void    GetEText( DialogPtr dPtr, int16 item, char *str )
  116. {
  117. int16      theType;
  118. Handle     theItem;
  119. Rect     theBox;
  120.         
  121.     GetDItem (dPtr, item, &theType, &theItem, &theBox);
  122.     if( theItem != NULL )
  123.         GetIText (theItem, str);
  124. }
  125.  
  126. /*******************************************************************/
  127.  
  128. void        SetCheck( dPtr, ChkItem, Value )
  129. DialogPtr    dPtr;
  130. int16            ChkItem;
  131. int16            Value;
  132. {
  133. int16        theType;
  134. Handle    theItem;
  135. Rect    theBox;
  136.  
  137.     if( Value )
  138.         Value = 1;
  139.  
  140.     GetDItem( dPtr, ChkItem, &theType, &theItem, &theBox );
  141.     SetCtlValue( theItem, Value );
  142. }
  143.  
  144. /*******************************************************************/
  145.  
  146. void        GetCheck( dPtr, ChkItem, Value )
  147. DialogPtr     dPtr;
  148. int16         ChkItem;
  149. int16             *Value;
  150. {
  151. int16     theType;
  152. Handle     theItem;
  153. Rect     theBox;
  154.  
  155.     GetDItem (dPtr, ChkItem, &theType, &theItem, &theBox);
  156.     *Value = GetCtlValue( theItem );
  157. }
  158.  
  159. /*******************************************************************/
  160.  
  161. Boolean    GoodNum( numStr )
  162. char     *numStr;
  163. {
  164. register int16    i, last = numStr[0];
  165. register unsigned char  c;
  166.  
  167.     for (i = 1; i <= last; i++)
  168.         {
  169.         c = numStr[i];
  170.         if((c < '0') || (c > '9'))
  171.             return FALSE;
  172.         }
  173.  
  174.     return TRUE;
  175. }
  176.  
  177. Boolean    GoodHex( numStr )
  178. char     *numStr;
  179. {
  180. register int16    i, last = numStr[0];
  181. register unsigned char  c;
  182.  
  183.     for (i = 1; i <= last; i++)
  184.         {
  185.         c = numStr[i];
  186.         if( ((c < '0') || (c > '9')) &&
  187.             ((c < 'A') || (c > 'F')) &&
  188.             ((c < 'a') || (c > 'f')) )
  189.             return FALSE;
  190.         }
  191.  
  192.     return TRUE;
  193. }
  194.  
  195. /*******************************************************************/
  196.  
  197. Boolean     IsTextLegalInt16( min, max, dPtr, dItem, value )
  198. int16        min, max;
  199. DialogPtr     dPtr;
  200. int16        dItem;
  201. int16        *value;
  202. {
  203. char    tempNum[ 256 ];
  204. int16    temp;
  205. int32    tempVal;
  206.  
  207.     GetEText( dPtr, dItem, tempNum );
  208.     if( GoodNum( tempNum ) )
  209.         {
  210.         StringToNum( &tempNum, &tempVal );
  211.         temp = (int16) tempVal;
  212.         if( temp >= min && temp <= max )
  213.             {
  214.             *value = temp;
  215.             return( TRUE );
  216.             }
  217.         }
  218.     SelIText( dPtr, dItem, 0, 32767 );
  219.     return( FALSE );
  220. }
  221.  
  222. /*******************************************************************/
  223.  
  224. Boolean     IsTextLegalInt32( min, max, dPtr, dItem, value )
  225. int32        min, max;
  226. DialogPtr     dPtr;
  227. int16        dItem;
  228. int32        *value;
  229. {
  230. char    tempNum[256];
  231. int32    temp;
  232. int32    tempVal;
  233.  
  234.     GetEText( dPtr, dItem, tempNum );
  235.     if( GoodNum( tempNum ) )
  236.         {
  237.         StringToNum( &tempNum, &tempVal );
  238.         temp = tempVal;
  239.         if( temp >= min && temp <= max )
  240.             {
  241.             *value = temp;
  242.             return( TRUE );
  243.             }
  244.         }
  245.         
  246.     SelIText( dPtr, dItem, 0, 32767 );
  247.     return( FALSE );
  248. }
  249.  
  250. /*******************************************************************/
  251.  
  252. void         pushradiobutton( thedialog, itemhit, first, last )
  253. DialogPtr     thedialog;
  254. int         itemhit,first,last;
  255. {
  256. int16         itemtype,i;
  257. Handle         itemhandle;
  258. Rect         itemrect;
  259.  
  260.     if( itemhit < first || itemhit > last || first < 0 )
  261.         return;
  262.         
  263.     for( i = first; i <= last; i++ )
  264.         {
  265.         GetDItem( thedialog, i, &itemtype, &itemhandle, &itemrect );
  266.         if( i == itemhit )
  267.             SetCtlValue( itemhandle, 1 );
  268.         else
  269.             SetCtlValue( itemhandle, 0 );
  270.         }
  271. }
  272.  
  273. /*******************************************************************/
  274.  
  275. void        OutlineButton( DialogBox, CornerRad )
  276. DialogPtr    DialogBox;
  277. int16        CornerRad;
  278. {
  279. Handle         itemhandle;
  280. Rect         itembox;
  281. GrafPtr     TempPort;
  282. int16        itemtype;
  283.  
  284.     GetPort( &TempPort );
  285.     SetPort( DialogBox );
  286.  
  287.     GetDItem( DialogBox, 1, &itemtype, &itemhandle, &itembox );
  288.     PenSize( 3,3 );
  289.     InsetRect( &itembox, -4, -4 );
  290.     FrameRoundRect( &itembox, CornerRad, CornerRad );
  291.     PenNormal(); 
  292.     SetPort( TempPort );
  293. }
  294.  
  295. /*******************************************************************/
  296.  
  297. extern    GrafPtr    screenPort;
  298.  
  299. CenterDialog( DPtr )
  300. GrafPtr        DPtr;    /* Note : a grafport in a window rec in a dialog rec */
  301. {
  302. Rect    D, N, S;
  303. int16    sx, sy, dx, dy;
  304.  
  305.     D = DPtr->portRect;
  306.     S = screenPort->portRect;
  307.     
  308.     dx = D.right - D.left;
  309.     dy = D.bottom - D.top;
  310.     
  311.     sx = S.right - S.left;
  312.     sy = S.bottom - S.top;
  313.     
  314.     N.left = ( sx - dx ) / 2;
  315.     N.top = ( sy - dy ) / 3;
  316.     if( N.top < 20 )
  317.         N.top = 20;
  318.     
  319.     MoveWindow( DPtr, N.left, N.top, false );
  320.     ShowWindow( DPtr );
  321. }
  322.  
  323. /*******************************************************************/
  324.  
  325. Boolean        DialogAsk( char *ask, char *answer )
  326. {
  327. DialogPtr     dPtr;
  328. int16        itemHit;
  329. char        buf[ 256 ];
  330.  
  331.  
  332.     dPtr = GetNewDialog( 1011, NULL, -1L );
  333.     if( dPtr == NULL )
  334.         return;
  335.     
  336.     strcpy( buf, ask );
  337.     CtoPstr( buf );
  338.     
  339.     ParamText( buf, NULL, NULL, NULL );
  340.     
  341.     SetArrow();
  342.     CenterDialog( dPtr );
  343.     OutlineButton( dPtr, 15 );
  344.  
  345.     while( 1 )
  346.         {
  347.         ModalDialog( NULL, &itemHit );
  348.         
  349.         if( itemHit == OK || itemHit == Cancel )
  350.             break;
  351.         }
  352.     
  353.     if( itemHit == OK )
  354.         {
  355.         GetEText( dPtr, 4, answer );
  356.         PtoCstr( answer );
  357.         DisposDialog( dPtr );
  358.         return( TRUE );
  359.         }
  360.     else
  361.         {
  362.         strcpy( answer, "" );
  363.         DisposDialog( dPtr );
  364.         return( FALSE );
  365.         }
  366. }